package com.ruoyi.asset.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.asset.common.AssetStatusType;
import com.ruoyi.asset.common.AssetTransactionRecordType;
import com.ruoyi.asset.common.DisposalType;
import com.ruoyi.asset.common.MaintenanceType;
import com.ruoyi.asset.domain.AssetTransactionRecord;
import com.ruoyi.asset.domain.FixedAssets;
import com.ruoyi.asset.mapper.FixedAssetsMapper;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.DisposalRecordMapper;
import com.ruoyi.asset.domain.DisposalRecord;
import com.ruoyi.asset.service.IDisposalRecordService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 报废处理记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@Service
public class DisposalRecordServiceImpl extends ServiceImpl<DisposalRecordMapper, DisposalRecord> implements IDisposalRecordService {
    @Autowired
    private DisposalRecordMapper disposalRecordMapper;

    @Autowired
    private FixedAssetsMapper fixedAssetsMapper;

    /**
     * 查询报废处理记录
     *
     * @param disposalId 报废处理记录主键
     * @return 报废处理记录
     */
    @Override
    public DisposalRecord selectDisposalRecordByDisposalId(String disposalId) {
        return disposalRecordMapper.selectDisposalRecordByDisposalId(disposalId);
    }

    /**
     * 查询报废处理记录列表
     *
     * @param disposalRecord 报废处理记录
     * @return 报废处理记录
     */
    @Override
    public List<DisposalRecord> selectDisposalRecordList(DisposalRecord disposalRecord) {
        return disposalRecordMapper.selectDisposalRecordList(disposalRecord);
    }

    /**
     * 新增报废处理记录
     *
     * @param disposalRecord 报废处理记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDisposalRecord(DisposalRecord disposalRecord) {
        //如果没有处置编号就自动生成一个处置编号
        String id = disposalRecord.getDisposalId();
        if (StringUtils.isEmpty(id)) {
            disposalRecord.setDisposalId(UUID.randomUUID().toString());
        }
        LambdaQueryWrapper<DisposalRecord> drlqWrapper = new LambdaQueryWrapper<>();

        //查询资产是否存在
        String assetNumber = disposalRecord.getAssetNumber();
        drlqWrapper.eq(DisposalRecord::getAssetNumber, assetNumber);
        drlqWrapper.ne(DisposalRecord::getDisposalType, DisposalType.CANCLE);

        Integer integer = disposalRecordMapper.selectCount(drlqWrapper);
        if (integer > 0) {
            throw new UtilException("该编码已存在在报废处理记录内,不能多次报废");
        }
        FixedAssets fixedAssets = null;

        fixedAssets = fixedAssetsMapper.selectById(assetNumber);

        if (fixedAssets == null) {
            throw new UtilException("该资产编号不存在");
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.MAINTENANCE)) {
            throw new UtilException(assetNumber + "正在维修不能新建报废记录");
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.TO_BE_SCRAPPED)) {
            throw new UtilException(assetNumber + "是待报废状态不能下新建报废记录");
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.SCRAPPED)) {
            throw new UtilException(assetNumber + "是报废状态不能下新建报废记录");
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.NORMAL) &&
        Objects.equals(disposalRecord.getDisposalType(),DisposalType.CANCLE)) {
            throw new UtilException(assetNumber + "是正常状态不需要取消报废");
        }

        String disposalType = disposalRecord.getDisposalType();
        if (Objects.equals(disposalType, DisposalType.TO_BE_SCRAPPED)) {
            fixedAssets.setAssetStatus(AssetStatusType.TO_BE_SCRAPPED);
            fixedAssetsMapper.updateById(fixedAssets);
        }

        if (Objects.equals(disposalType, DisposalType.SCRAPPED)) {
            fixedAssets.setAssetStatus(AssetStatusType.SCRAPPED);
            fixedAssetsMapper.updateById(fixedAssets);
        }

        return disposalRecordMapper.insert(disposalRecord);
    }

    /**
     * 修改报废处理记录
     *
     * @param disposalRecord 报废处理记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDisposalRecord(DisposalRecord disposalRecord) {


        DisposalRecord oldDisposalRecord = disposalRecordMapper.selectById(disposalRecord.getDisposalId());
        if (!oldDisposalRecord.getDisposalType().equals(DisposalType.TO_BE_SCRAPPED)) {
            throw new RuntimeException("只有待报废状态才能更改");
        }

        String assetNumber = disposalRecord.getAssetNumber();
        FixedAssets fixedAssets = fixedAssetsMapper.selectById(assetNumber);

        String disposalType = disposalRecord.getDisposalType();
        if (Objects.equals(disposalType, DisposalType.TO_BE_SCRAPPED)) {
            fixedAssets.setAssetStatus(AssetStatusType.TO_BE_SCRAPPED);
            fixedAssetsMapper.updateById(fixedAssets);
        }

        if (Objects.equals(disposalType, DisposalType.SCRAPPED)) {
            fixedAssets.setAssetStatus(AssetStatusType.SCRAPPED);
            fixedAssetsMapper.updateById(fixedAssets);
        }

        if (Objects.equals(disposalType, DisposalType.CANCLE)) {
            if (disposalRecord.getRemark() == null ||disposalRecord.getRemark().equals("")) {
                throw new UtilException("请说明取消报废的原因！");
            }
            fixedAssets.setAssetStatus(AssetStatusType.NORMAL);
            fixedAssetsMapper.updateById(fixedAssets);
        }

        return disposalRecordMapper.updateById(disposalRecord);
    }

    /**
     * 批量删除报废处理记录
     *
     * @param disposalIds 需要删除的报废处理记录主键
     * @return 结果
     */
    @Override
    public int deleteDisposalRecordByDisposalIds(String disposalIds) {
        return disposalRecordMapper.deleteDisposalRecordByDisposalIds(Convert.toStrArray(disposalIds));
    }

    /**
     * 删除报废处理记录信息
     *
     * @param disposalId 报废处理记录主键
     * @return 结果
     */
    @Override
    public int deleteDisposalRecordByDisposalId(String disposalId) {

        return disposalRecordMapper.deleteDisposalRecordByDisposalId(disposalId);
    }

    @Override
    public String disposal(DisposalRecord disposalRecord) {
        //如果没有处置编号就自动生成一个处置编号
        String id = disposalRecord.getDisposalId();
        if (StringUtils.isEmpty(id)) {
            disposalRecord.setDisposalId(UUID.randomUUID().toString());
        }
        LambdaQueryWrapper<DisposalRecord> drlqWrapper = new LambdaQueryWrapper<>();

        //查询资产是否存在
        String assetNumber = disposalRecord.getAssetNumber();
        drlqWrapper.eq(DisposalRecord::getAssetNumber, assetNumber);
        drlqWrapper.ne(DisposalRecord::getDisposalType, DisposalType.CANCLE);

        Integer integer = disposalRecordMapper.selectCount(drlqWrapper);
        if (integer > 0) {
            return "该编码已存在在报废处理记录内,不能多次报废";
        }
        FixedAssets fixedAssets = null;

        fixedAssets = fixedAssetsMapper.selectById(assetNumber);

        if (fixedAssets == null) {
            return "该资产编号不存在";
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.MAINTENANCE)) {
            return assetNumber + "正在维修不能新建报废记录";
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.TO_BE_SCRAPPED)) {
            return assetNumber + "是待报废状态不能下新建报废记录";
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.SCRAPPED)) {
            return  assetNumber + "是报废状态不能下新建报废记录";
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.NORMAL) &&
                Objects.equals(disposalRecord.getDisposalType(),DisposalType.CANCLE)) {
           return assetNumber + "是正常状态不需要取消报废";
        }

        String disposalType = disposalRecord.getDisposalType();
        if (Objects.equals(disposalType, DisposalType.TO_BE_SCRAPPED)) {
            fixedAssets.setAssetStatus(AssetStatusType.TO_BE_SCRAPPED);
            fixedAssetsMapper.updateById(fixedAssets);
        }

        if (Objects.equals(disposalType, DisposalType.SCRAPPED)) {
            fixedAssets.setAssetStatus(AssetStatusType.SCRAPPED);
            fixedAssetsMapper.updateById(fixedAssets);
        }

        return null;
    }



}
